#-*- coding:utf-8 -*-

import os
from pychartdir import * 
import recorder
import wx
import wx.aui
import wx.combo
import wx.grid


ID_Exit = wx.NewId()
ID_FileOpen = wx.NewId()
ID_SearchSearch = wx.NewId()
ID_About = wx.NewId()


class RecoderFrame(wx.Frame):
	def __init__(self, parent):
		wx.Frame.__init__(self, parent, -1, "Recorder", size=(640, 480))
		self.record = recorder.JamongRecord()
		
		ft = self.GetFont()
		ft.SetPointSize(8)
		self.SetFont(ft)
		
		self.CreateStatusBar()
		self.CreateToolBar()

		self.createMenu()
		self.createPanel()
		
		
	def createMenu(self):
		mb = wx.MenuBar()

		# File Menu
		m = wx.Menu()
		mi = wx.MenuItem(m, ID_FileOpen, "열기(&O)\tCtrl+O")
#		mi.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN))
		m.AppendItem(mi)
		m.AppendSeparator()
		m.Append(ID_Exit, "끝내기(&X)")
		mb.Append(m, "파일(&F)")
		
		# Search Menu
		m = wx.Menu()
		mi = wx.MenuItem(m, ID_SearchSearch, "찾기(&S)\tCtrl+F")
#		mi.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_FIND))
		m.AppendItem(mi)
		mb.Append(m, "편집(&E)")
		
		# Help Menu
		m = wx.Menu()
		m.Append(ID_About, "정보(&A)")
		mb.Append(m, "도움말(&H)")
		self.SetMenuBar(mb)

        # Event binding		
		self.Bind(wx.EVT_MENU, self.onFileOpen, id = ID_FileOpen)
		self.Bind(wx.EVT_MENU, self.onSearchSearch, id = ID_SearchSearch)
		self.Bind(wx.EVT_MENU, self.onExit, id = ID_Exit )

		self.Bind(wx.EVT_FIND, self.onFind)
		self.Bind(wx.EVT_FIND_NEXT, self.onFind)
		
		
	def createPanel(self):
		self._mgr = wx.aui.AuiManager()
		_mgr = self._mgr
		_mgr.SetManagedWindow(self)
		
		self.lc = wx.grid.Grid(self, -1)
		self.lc.CreateGrid(1, 1, wx.grid.Grid.wxGridSelectRows)
		self.lc.InsertCols(0, len(self.record.getTitles()))
		for n, tit in enumerate(self.record.getTitles()):
			self.lc.SetColLabelValue(n, tit)
			
		_mgr.AddPane(self.lc, wx.aui.AuiPaneInfo().CenterPane())

#		self.lcb = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
#		self.lcb.InsertColumn(1, "")
#		self.lcb.InsertColumn(2, "")
		
		self.bc = BinaryChartPanel(self)

		_mgr.AddPane(self.bc, wx.aui.AuiPaneInfo().Bottom().
					Caption("Result").MaximizeButton().
					CloseButton(False))
		

		_mgr.Update()
	

	def onFileOpen(self, event):
		dlg = wx.FileDialog(
			self,
			wildcard="Jamong Files(*.xls)|*.xls|All Files(*.*)|*.*",
			style=wx.OPEN | wx.CHANGE_DIR
			)
				
		if dlg.ShowModal() == wx.ID_OK:
			paths = dlg.GetPath()
			self.record.loadFile(paths)
			self.listOut()

		dlg.Destroy()

	
	def listOut(self):
		self.lc.DeleteRows(0, self.lc.GetNumberRows())
		self.lc.InsertRows(0, self.record.count())
		for n, rc in enumerate(self.record.getRecords()):
			for ni, item in enumerate(rc):
				self.lc.SetCellValue(n, ni, unicode(item, "cp949"))
			

	def onSearchSearch(self, event):
		dlg = FindDialog(self, self.record)
		dlg.Show()
			
	
	
	def onExit(self, event):
		self.Close()
		

	def onFind(self, event):
		if event.GetEventType() == wx.wxEVT_COMMAND_FIND:
			print "BB"
	

class FindDialog(wx.Dialog):
	condlist = [
			"다음 값과 같음",
			"다음 값보다 작음",
			"다음 값보다 작거나 같음",
			"다음 값보다 큼",
			"다음 값보다 크거나 같음",
			"다음 값과 다름"
			]
	
	
	def __init__(self, parent, record):
		wx.Dialog.__init__(self, parent, title="찾기")
		self.record = record
		
		# Set Font
		fc = self.GetFont()
		fc.SetPointSize(8)
		self.SetFont(fc)

		mainSizer = wx.BoxSizer(wx.VERTICAL)
		stboxSizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, "조건"), wx.VERTICAL)

		# Search criteria
		for n in range(3):
			sizer = wx.BoxSizer(wx.HORIZONTAL)
			cb = wx.Choice(self, -1, choices=self.record.getTitles())
			sizer.Add(cb, 0, wx.ALL, 5)
			sizer.Add(wx.StaticText(self, -1, "이"),  0, wx.ALL, 5)
			cbCond = wx.Choice(self, -1, choices=self.condlist)
			sizer.Add(cbCond, 0, wx.ALL, 5)
			tc = wx.TextCtrl(self, -1)
			sizer.Add(tc, 0, wx.ALL, 5)
			stboxSizer.Add(sizer)
		mainSizer.Add(stboxSizer, 0, wx.ALL, 5)
		
		# Standard Dialog Button
		sizer = wx.StdDialogButtonSizer()
		btn = wx.Button(self, wx.ID_YES, "찾기")
		btn.SetDefault()
		sizer.AddButton(btn)
		self.Bind(wx.EVT_BUTTON, self.onFind, btn)
		btn = wx.Button(self, wx.ID_CANCEL, "닫기")
		sizer.AddButton(btn)
		sizer.Realize()
		mainSizer.Add(sizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
		
		self.SetSizer(mainSizer)
		mainSizer.Fit(self)
		
		
	def onFind(self, event):
		evt = wx.FindDialogEvent(wx.wxEVT_COMMAND_FIND, self.GetId())
		self.GetParent().GetEventHandler().ProcessEvent(evt)


class BinaryChartPanel(wx.Panel):
	def __init__(self, parent):
		wx.Panel.__init__(self, parent, size=wx.Size(100,100))

		self.Bind(wx.EVT_PAINT, self.OnPaint)
                
	def setValue(self, value):
		self.value = value
		
	def OnPaint(self, evt):
		dc = wx.PaintDC(self)
		size = dc.GetSize()
		data = [85, 156, 179.5, 211, 123]
		labels = ["Mon", "Tue", "Wed", "Thu", "Fri"]
		
		c = XYChart(550, 250)
		c.setPlotArea(30, 20, 500, 200)
		c.addStepLineLayer(data)
		c.addLineLayer(data)
		c.xAxis().setLabels(labels)
		ch = c.makeTmpFile(".", BMP)
		bmp = wx.Bitmap(ch)
		dc.DrawBitmap(bmp, 0, 0)
		os.remove(ch)

		
		
	
		
class NumericChartPanel(wx.Panel):
	def __init__(self, parent):
		wx.Panel.__init__(self, parent)
		
				
				
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = RecoderFrame(None)
    frame.Show(True)
    app.MainLoop()
	